import sys
from typing import Tuple
import pygame


from game.ui.widgets.image import ImageWidget
from game.ui.widgets.text import TextWidget
from game.ui.ui import UI
from game.data.assets.assetManager import load_image
from game.sound import MenuMusicManager, GameMusicManager


class MainMenu(UI):
    def __init__(self,
                 groups: Tuple[pygame.sprite.LayeredUpdates, ...],
                 ):
        super().__init__(groups)

        # ====================
        # = Background Image =
        # ====================
        self.background = ImageWidget(
            self.display_surface,
            load_image("test/ground.png"),
            self.display_surface.get_rect().center,
            self.groups)


        # ===================
        # = Start Game Text =
        # ===================

        def __callback_start_game_text():
            from game.event.eventManager import EventId, Eventbus
            from game.ui.menus import Menus


            Eventbus.triggerEvent(EventId.game_start)
            Eventbus.triggerEvent(EventId.toggle_game_pause)
            MenuMusicManager.stop()
            GameMusicManager.play()
            Menus.game_menu.component_list[0].undraw()

        TextWidget(
            self.display_surface,
            (200, 100),
            self.groups,
            "开始游戏",
            pygame.Color(0, 0, 0),
            pygame.Color(255, 255, 255),
            on_click_callbacks = (__callback_start_game_text, )
        )

        # ===================
        # = Quit Text =
        # ===================
        def __callback_quit_text():
            pygame.quit()
            sys.exit()

        TextWidget(
            self.display_surface,
            (200, 200),
            self.groups,
            "退出游戏",
            pygame.Color(0, 0, 0),
            pygame.Color(255, 255, 255),
            on_click_callbacks = (__callback_quit_text, )
        )

        # ===================
        # = Go to Settings Text =
        # ===================
        def __callback_goto_settings_text():
            from game.ui.menus import Menus
            self.jumpto(Menus.settings_menu, undraw_bg=False)  # type: ignore

        TextWidget(self.display_surface,
                   (200, 300),
                   self.groups,
                   "设置",
                   pygame.Color(0, 0, 0),
                   pygame.Color(255, 255, 255),
                   on_click_callbacks = (__callback_goto_settings_text, ))

    def update(self) -> None:
        if not MenuMusicManager.is_channel_busy():
            MenuMusicManager.play()

        super().update()